package com.hay.auth.controller;

import com.hay.auth.common.entity.UserInfo;
import com.hay.auth.common.utils.JwtUtils;
import com.hay.auth.config.JwtProperties;
import com.hay.auth.service.AuthService;
import com.hay.common.utils.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @title: AuthController
 * @Author HuangYan
 * @Date: 2020/8/4 15:08
 * @Version 1.0
 */
@Controller
@EnableConfigurationProperties(JwtProperties.class)
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 获取token信息
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     */
    @PostMapping("accredit")
    public ResponseEntity<Void> accredit(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        // 调用service方法，返回一个jwt
        String token = authService.accredit(username, password);
        if (StringUtils.isBlank(token)){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        // 将token放入cookie中
        CookieUtils.setCookie(request, response, jwtProperties.getCookieName(), token,jwtProperties.getExpire() * 60);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("verify")
    public ResponseEntity<UserInfo> verify(
            @CookieValue("LY_TOKEN") String token,
            HttpServletRequest request,
            HttpServletResponse response
    ){

        try {
            // 解析token获取user信息
            UserInfo userInfo = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());

            // 判单是否存在
            if (StringUtils.isBlank(userInfo.getUsername()) || userInfo.getId() == null){
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            // 通过创建新的jwt来刷新token存活时间
            String newToken = JwtUtils.generateToken(userInfo, jwtProperties.getPrivateKey(), jwtProperties.getExpire());

            // 通过创建新cookie来刷新cookie存活时间
            CookieUtils.setCookie(request, response, jwtProperties.getCookieName(), token, jwtProperties.getExpire() * 60);

            // 返回user
            return ResponseEntity.ok(userInfo);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }
}
